require(["common", "jQuery", "coAjax", "queryString", "hash_routie"], function(jSouper, $, coAjax, QueryString, hash_routie) {
	var _num;
	var _page;
	var _tag_id;
	var _qs;

	hash_routie.on("tile", function() {
		_qs = new QueryString(location.hash);
		var num = _qs.get("num") || 10;
		var page = _qs.get("page") || 0;
		var tag_id = _qs.get("tag_id");
		// 加载标签列表
		coAjax.get(appConfig.bus.tags, {
			bus_id: busInfo._id
		}, function(result) {
			var tag_list = result.result;
			App.set("$Cache.bus_tags_detail", tag_list);
		});

		//获取要编辑的标签
		if (tag_id !== _tag_id) {
			_tag_id = tag_id;
			_tag_id && coAjax.get(appConfig.bus.tags_detail_by_id, {
				tag_id: _tag_id,
				bus_id: busInfo._id
			}, function(result) {
				_refresh_editing_vm_data(result.result);
			});
		}


		// 分页加载商品列表
		if (num !== _num || page !== _page) {
			_num = num;
			_page = page;
			coAjax.get(appConfig.goods.list_with_page_info, {
				bus_id: busInfo._id,
				num: _num,
				page: _page
			}, function(result) {
				var info = result.result;
				App.set("$Cache.tile.goods_with_page_info", info);
			});
		}
	});
	//添加标签
	App.set("$Event.tag.add_new_tag", function() {
		var new_tag_name = $.trim(App.get("$Cache.tag.new_tag_name"));
		App.set("$Cache.tag.new_tag_name", "");
		if (new_tag_name) {
			coAjax.post(appConfig.bus.add_tag, {
				tag_name: new_tag_name
			}, function(result) {
				var bus_tags_detail = App.get("$Cache.bus_tags_detail") || [];
				bus_tags_detail.push(result.result);
				App.set("$Cache.bus_tags_detail", bus_tags_detail);
			});
		}
	});
	//修改标签次序
	App.set("$Event.tile.update_tag_index", function(e, vm) {
		var dragend_index = vm.get("$CPrivate.$Cache.draggable.dragend_index");
		coAjax.put(appConfig.bus.update_tag_index, {
			tag_id: App.get("$Cache.bus_tags_detail." + vm.get("$CPrivate.$Cache.draggable.dragging_index") + "._id"),
			tag_index: dragend_index
		}, function(result) {
			hash_routie.emit("tile");

			// App.set("$Cache.bus_tags_detail", result.result);
		});
	});

	function _refresh_editing_vm_data(data) {
		data || (data = App.get("$Cache.tile.editing_tag"));

		//限制显示的商品数量
		var detail_content = data.detail_content
		detail_content && (data.showing_content = detail_content.slice(0, ~~(data.showing_content ? data.showing_content.length : 10)));

		App.set("$Cache.tile.editing_tag", data);
	};
	// 点击分类成编辑状态
	App.set("$Event.tile.to_editing", function(e, vm) {
		hash_routie.hash({
			tag_id: vm.get("_id")
		});
	});
	//显示更多商品
	App.set("$Event.tile.editing_tag.show_more", function(e, vm) {
		data = App.get("$Cache.tile.editing_tag");

		var detail_content = data.detail_content
		detail_content && (data.showing_content = detail_content.slice(0, ~~(data.showing_content ? data.showing_content.length : 10) + 10));
		App.model.touchOff("$Cache.tile.editing_tag.showing_content")
	});
	// 删除标签
	App.set("$Event.tile.remove_tag", function() {
		myConfirm("确定要删除“" + App.get("$Cache.tile.editing_tag.tag_name") + "”这个标签吗？", function() {
			coAjax["delete"](appConfig.bus.remove_tag, {
				tag_id: App.get("$Cache.tile.editing_tag._id")
			}, function(result) {
				App.set("$Cache.bus_tags_detail", result.result);
				App.set("$Cache.tile.editing_tag", null);
			});
		});
	});
	// 删除标签中的商品
	App.set("$Event.tile.remove_goods_from_tag", function(e, vm) {
		coAjax["delete"](appConfig.bus.remove_goods_from_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			goods_id: vm.get("_id")
		}, function(result) {
			_refresh_editing_vm_data(result.result);
		});
	});
	// 添加标签中的商品
	App.set("$Event.tile.add_goods_to_tag", function(e, vm) {
		coAjax.post(appConfig.bus.add_goods_to_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			goods_id: vm.get("_id")
		}, function(result) {
			_refresh_editing_vm_data(result.result);
		});
	});
	//进行标签重命名
	App.set("$Event.tile.to_rename_tag", function() {
		App.set("$Cache.tile.is_in_rename_tag", true);
		//缓存标签名
		App.set("$Cache.tile.rename_tag_bak", App.get("$Cache.tile.editing_tag.tag_name"));
	});
	//提交重命名
	App.set("$Event.tile.submit_new_tag_name", function() {
		coAjax.put(appConfig.bus.rename_tag, {
			tag_id: App.get("$Cache.tile.editing_tag._id"),
			tag_name: App.get("$Cache.tile.editing_tag.tag_name")
		}, function(result) {
			hash_routie.emit("tile");
			_refresh_editing_vm_data(result.result);
			App.set("$Cache.tile.is_in_rename_tag", false);
		});
	});
	// 取消重命名
	App.set("$Event.tile.cancel_new_tag_name", function() {
		App.set("$Cache.tile.editing_tag.tag_name", App.get("$Cache.tile.rename_tag_bak"));
		App.set("$Cache.tile.is_in_rename_tag", false);
	});

	//搜索商品
	var _search_ti;
	App.set("$Event.tile.search", function() {
		clearTimeout(_search_ti);
		_search_ti = setTimeout(function() {
			var search_text = jSouper.trim(App.get("$Cache.tile.search_text"));
			App.set("$Cache.tile.is_searching", !!search_text);
			if (search_text) {
				coAjax.get(appConfig.goods.search, {
					bus_id: busInfo._id,
					q: search_text
				}, function(result) {
					var goods_info_list = result.result;
					var goods_list = [];
					for (var i = 0, item; item = goods_info_list[i]; i += 1) {
						goods_list.push(item.goods);
					}
					App.set("$Cache.tile.search_goods", goods_list);
				});
			}
		}, 120);
	});
});